# This file is part of BlackArch Linux ( https://www.blackarch.org/ ).
# See COPYING for license details.

pkgname=recaf
pkgver=4.0.0.alpha.r113.ge9570b0
_javaver=24
pkgrel=1
groups=('blackarch' 'blackarch-decompiler' 'blackarch-reversing')
pkgdesc='Modern Java bytecode editor.'
arch=('any')
url='https://github.com/Col-E/Recaf'
license=('MIT')
depends=('java-runtime' 'java-environment' "java$_javaver-openjfx-bin")
makedepends=('git' 'jdk-openjdk' "gradle")
source=("$pkgname::git+https://github.com/Col-E/Recaf.git")
sha512sums=('SKIP')

pkgver() {
  cd $pkgname

  ( set -o pipefail
    git describe --long --tags --abbrev=7 2>/dev/null |
      sed 's/\([^-]*-g\)/r\1/;s/-/./g' ||
    printf "%s.%s" "$(git rev-list --count HEAD)" \
      "$(git rev-parse --short=7 HEAD)"
  )
}

prepare() {
  cd $pkgname

  echo "org.gradle.java.installations.auto-detect=false" >> gradle.properties
  echo "org.gradle.java.installations.auto-download=false" >> gradle.properties
}

build() {
  cd $pkgname

  # How upstream builds
  # https://github.com/Col-E/Recaf/blob/master/.github/workflows/build.yml

  # Try either with
  # 1. gradle as makedepends + call gradle
  # 2. no gradle as makedepends + call ./gradlew
  # `./gradlew` wrapper forces a specific version of gradle that may not be
  # compatible with latest Java version
  # See https://docs.gradle.org/current/userguide/compatibility.html

  TARGET_VERSION=$_javaver gradle publish --stacktrace
}

package() {
  cd $pkgname

  install -dm 755 "$pkgdir/usr/bin"
  install -dm 755 "$pkgdir/usr/share/$pkgname"

  cp recaf-ui/build/libs/recaf-ui-*-all.jar \
    "$pkgdir/usr/share/$pkgname/$pkgname.jar"

  cat > "$pkgdir/usr/bin/$pkgname" << EOF
#!/bin/sh
java -jar /usr/share/$pkgname/$pkgname.jar "\$@"
EOF

  chmod +x "$pkgdir/usr/bin/$pkgname"
}

